#include <iostream>
#include <queue>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
using namespace std;
const int maxn = 1e6+7;
int node[maxn][26], cnt=0, num[maxn];
int value[maxn];
void Init() {
    memset(node[0], 0, sizeof(node[0]));
    cnt = 0;
    value[0] = 0;
}
void Insert(char* s) {
    int now=0;
    int len=strlen(s);
    int mark = -1;
    for (int i=0; i<len; ++i) {
        int t = s[i] - 'a';
        int &k = node[now][t];
        if (!k) {
            ++cnt;
            num[cnt] = 0;
            value[cnt] = value[now] + 1;
            memset(node[cnt], 0, sizeof(node[cnt]));
            k = cnt;
            if (mark==-1) mark = value[k];
        }
        ++num[k];
        now = k;
    }
    int last = now;
    if (mark != -1) value[last] = min(value[last], mark + 1);
    now = 0;
    len -= 1;
    for (int i=0; i<len; ++i) {
        int t = s[i] - 'a';
        int k = node[now][t];
        if (num[k] == 1) {
            value[k] = min(value[k], value[last] + len - i);
        }
        now = k;
    }
}
int solve(char* s) {
    int res=0;
    int len = strlen(s);
    int now = 0;
    int i = 0;
    for (; i<len; ++i) {
        int t = s[i] - 'a';
        int k = node[now][t];
        if (!k) break;
        res = value[k];
        now = k;
    }
    res += (len-i);
    return res;
}
char ss[maxn];
int main(int argc, char** argv) {
    #ifdef hai
    freopen("in.txt", "r", stdin);
    #endif
    int n,m;
    char c;
    while (cin>>n>>m) {
        getchar();
        Init();
        for (int i=0; i<n; ++i) {
            int t=0;
            while (c=getchar()) {
                if (c=='\n') break;
                ss[t++] = c;
            }
            ss[t] = '\0';
            //cout<<ss<<endl;
            Insert(ss);
        }
        for (int i=0; i<m; ++i) {
            int t=0;
            while (c=getchar()) {
                if (c=='\n') break;
                ss[t++] = c;
            }
            ss[t] = '\0';
            //cout<<ss<<endl;
            cout<<solve(ss)<<endl;
        }
        cout<<endl;
    }
    return 0;
}
